
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.template.response &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-template-response">
            
  <h1>Source code for django.template.response</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">six</span>

<span class="kn">from</span> <span class="nn">.loader</span> <span class="k">import</span> <span class="n">get_template</span><span class="p">,</span> <span class="n">select_template</span>


<span class="k">class</span> <span class="nc">ContentNotRenderedError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>


<div class="viewcode-block" id="SimpleTemplateResponse"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.SimpleTemplateResponse">[docs]</a><span class="k">class</span> <span class="nc">SimpleTemplateResponse</span><span class="p">(</span><span class="n">HttpResponse</span><span class="p">):</span>
    <span class="n">rendering_attrs</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;template_name&#39;</span><span class="p">,</span> <span class="s1">&#39;context_data&#39;</span><span class="p">,</span> <span class="s1">&#39;_post_render_callbacks&#39;</span><span class="p">]</span>

<div class="viewcode-block" id="SimpleTemplateResponse.__init__"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.SimpleTemplateResponse.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">content_type</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">charset</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># It would seem obvious to call these next two members &#39;template&#39; and</span>
        <span class="c1"># &#39;context&#39;, but those names are reserved as part of the test Client</span>
        <span class="c1"># API. To avoid the name collision, we use different names.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">template_name</span> <span class="o">=</span> <span class="n">template</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">context_data</span> <span class="o">=</span> <span class="n">context</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">using</span> <span class="o">=</span> <span class="n">using</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_post_render_callbacks</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># _request stores the current request object in subclasses that know</span>
        <span class="c1"># about requests, like TemplateResponse. It&#39;s defined in the base class</span>
        <span class="c1"># to minimize code duplication.</span>
        <span class="c1"># It&#39;s called self._request because self.request gets overwritten by</span>
        <span class="c1"># django.test.client.Client. Unlike template_name and context_data,</span>
        <span class="c1"># _request should not be considered part of the public API.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># content argument doesn&#39;t make sense here because it will be replaced</span>
        <span class="c1"># with rendered template so we always pass empty string in order to</span>
        <span class="c1"># prevent errors and provide shorter signature.</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SimpleTemplateResponse</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">content_type</span><span class="p">,</span> <span class="n">status</span><span class="p">,</span> <span class="n">charset</span><span class="o">=</span><span class="n">charset</span><span class="p">)</span>

        <span class="c1"># _is_rendered tracks whether the template and context has been baked</span>
        <span class="c1"># into a final response.</span>
        <span class="c1"># Super __init__ doesn&#39;t know any better than to set self.content to</span>
        <span class="c1"># the empty string we just gave it, which wrongly sets _is_rendered</span>
        <span class="c1"># True, so we initialize it to False after the call to super __init__.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span> <span class="o">=</span> <span class="kc">False</span></div>

    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Raise an exception if trying to pickle an unrendered response. Pickle</span>
<span class="sd">        only rendered data, not the data used to construct the response.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">obj_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ContentNotRenderedError</span><span class="p">(</span><span class="s1">&#39;The response content must be &#39;</span>
                                          <span class="s1">&#39;rendered before it can be pickled.&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">attr</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">rendering_attrs</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">attr</span> <span class="ow">in</span> <span class="n">obj_dict</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">obj_dict</span><span class="p">[</span><span class="n">attr</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">obj_dict</span>

<div class="viewcode-block" id="SimpleTemplateResponse.resolve_template"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.SimpleTemplateResponse.resolve_template">[docs]</a>    <span class="k">def</span> <span class="nf">resolve_template</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template</span><span class="p">):</span>
        <span class="s2">&quot;Accepts a template object, path-to-template or list of paths&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">template</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">return</span> <span class="n">select_template</span><span class="p">(</span><span class="n">template</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">template</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">get_template</span><span class="p">(</span><span class="n">template</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">template</span></div>

<div class="viewcode-block" id="SimpleTemplateResponse.resolve_context"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.SimpleTemplateResponse.resolve_context">[docs]</a>    <span class="k">def</span> <span class="nf">resolve_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">context</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">rendered_content</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns the freshly rendered content for the template and context</span>
<span class="sd">        described by the TemplateResponse.</span>

<span class="sd">        This *does not* set the final content of the response. To set the</span>
<span class="sd">        response content, you must either call render(), or set the</span>
<span class="sd">        content explicitly using the value of this property.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">template</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">resolve_template</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">template_name</span><span class="p">)</span>
        <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">resolve_context</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context_data</span><span class="p">)</span>
        <span class="n">content</span> <span class="o">=</span> <span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_request</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">content</span>

<div class="viewcode-block" id="SimpleTemplateResponse.add_post_render_callback"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.SimpleTemplateResponse.add_post_render_callback">[docs]</a>    <span class="k">def</span> <span class="nf">add_post_render_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callback</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Adds a new post-rendering callback.</span>

<span class="sd">        If the response has already been rendered,</span>
<span class="sd">        invoke the callback immediately.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span><span class="p">:</span>
            <span class="n">callback</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_post_render_callbacks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">callback</span><span class="p">)</span></div>

<div class="viewcode-block" id="SimpleTemplateResponse.render"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.SimpleTemplateResponse.render">[docs]</a>    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Renders (thereby finalizing) the content of the response.</span>

<span class="sd">        If the content has already been rendered, this is a no-op.</span>

<span class="sd">        Returns the baked response instance.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">retval</span> <span class="o">=</span> <span class="bp">self</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">content</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rendered_content</span>
            <span class="k">for</span> <span class="n">post_callback</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_post_render_callbacks</span><span class="p">:</span>
                <span class="n">newretval</span> <span class="o">=</span> <span class="n">post_callback</span><span class="p">(</span><span class="n">retval</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">newretval</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">retval</span> <span class="o">=</span> <span class="n">newretval</span>
        <span class="k">return</span> <span class="n">retval</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_rendered</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ContentNotRenderedError</span><span class="p">(</span>
                <span class="s1">&#39;The response content must be rendered before it can be iterated over.&#39;</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">SimpleTemplateResponse</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__iter__</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">content</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ContentNotRenderedError</span><span class="p">(</span>
                <span class="s1">&#39;The response content must be rendered before it can be accessed.&#39;</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">SimpleTemplateResponse</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">content</span>

    <span class="nd">@content</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">content</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Sets the content for the response</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">HttpResponse</span><span class="o">.</span><span class="n">content</span><span class="o">.</span><span class="n">fset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_is_rendered</span> <span class="o">=</span> <span class="kc">True</span></div>


<div class="viewcode-block" id="TemplateResponse"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.TemplateResponse">[docs]</a><span class="k">class</span> <span class="nc">TemplateResponse</span><span class="p">(</span><span class="n">SimpleTemplateResponse</span><span class="p">):</span>
    <span class="n">rendering_attrs</span> <span class="o">=</span> <span class="n">SimpleTemplateResponse</span><span class="o">.</span><span class="n">rendering_attrs</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;_request&#39;</span><span class="p">]</span>

<div class="viewcode-block" id="TemplateResponse.__init__"><a class="viewcode-back" href="../../../ref/template-response.html#django.template.response.TemplateResponse.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">template</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">content_type</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">status</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">charset</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">TemplateResponse</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="n">template</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">content_type</span><span class="p">,</span> <span class="n">status</span><span class="p">,</span> <span class="n">charset</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="o">=</span> <span class="n">request</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>